<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>作品图片管理</title>
   <!-- bootstrap -->
   <script src="vendor/jquery/jquery.min.js"></script>
   <link href="vendor/bootstrap/css/bootstrap.min.css"  rel="stylesheet">
   <script src="vendor/bootstrap/js/bootstrap.min.js"></script>

   <!-- 主要css-->
   <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">

   <!-- vue和axios -->
   <script src="js/vue.min.js"></script>
   <script src="js/axios.min.js"></script>
   
  <!-- js -->
   <script src="js/header.js"></script>
</head>
<body>
    <script>
  
  window.onload = function () {
   if(sessionStorage.jwt != '123456'){
              window.location.href = 'admin_login.html';
            }
    
        var pid = getUrlParms("pid");
        Vue.prototype.$http = axios;
        new Vue({
          el: '#workArea',
          data: {
            data2:null,
            file: '',
            url:''
          },
          mounted:function(){ //mounted　表示这个 Vue 对象加载成功了
                            this.init();    
                            },
          methods: {
            clear:function () {
              $("#Pic").val('');
                                this.file=null;
                                },    
            init:function(){
                                 var uri='http://120.79.186.44:9092/manager/production/img/selById';
                                  var ur1=uri+"?id="+pid;
                                 axios.get(ur1)
                          .then(response => {
                            this.data2 = response.data;
                            })    //获取到的indexDatas赋值给data2
                          .catch(function (error) { // 请求失败处理
                            alert(0);
                            });
                                 },
                                //  删除
                                 deleteBean: function(id) {     
                                  var d = confirm("是否要删除");
                                  var uri='http://120.79.186.44:9092/manager/production/img/delete';
                                  var url=uri+ "?id="+id;
                                  if(d===true)               
                                   axios.post(url)
                                     .then(res => {
                                      if(res.status === 200) {
                                        alert("删除成功");
                                        this.init();
                                    }
                                    })
                                 },
        
            getFile(event) {
              this.file = event.target.files[0];
              console.log(this.file);
            },
            submitForm(event) {
              if(!checkEmpty(this.file, "比赛图片")){};
              event.preventDefault();
              let formData = new FormData();
              formData.append('file', this.file);
              let config = {
                headers: {
                  'Content-Type': 'multipart/form-data'
                }
              }
              this.$http.post('http://120.79.186.44:9092/manager/production/pic/upload', formData, config).then(res=>  {
                (this.url = res.data.url);  
                if (res.status === 200) {
                axios.post('http://120.79.186.44:9092/manager/production/img/insert', {pid:pid,img:this.url})
                               .then(res => {
                                    if(res.status === 200) {
                                        alert("添加成功");
                                        this.init();
                                        this.clear();
                                    }
                                  })  
                
                }
              });
       
            }
          }
        });
      }
           </script>      
    <div class="page"   id="workArea">
        <!--头部-->
        <header class="header">
            <nav class="navbar">
              <div class="container-fluid">
                <div class="navbar-holder d-flex align-items-center justify-content-between">
                  <div class="navbar-header">
                    <a href="#" class="navbar-brand d-none d-sm-inline-block">
                      <div class="brand-text d-none d-lg-inline-block"><span>USC-SE </span><strong>ADMIN</strong></div>
                    </a>
                  </div>

                </div>
              </div>
            </nav>
          </header>
       
        <div class="page-content d-flex align-items-stretch"> 
          
          
          <!-- 左部 -->
          <nav class="side-navbar">
            <!-- 头像名字部分-->
            <div class="sidebar-header d-flex align-items-center">
                <div class="avatar"><img src="img/admin.jpg" alt="..." class="img-fluid rounded-circle"></div>
                <div class="title">
                  <h1 class="h4">USC</h1>
                  <p>Welcome</p>
                </div>
            </div>
  
            <!-- 左边菜单-->
            <span class="heading">管理</span>
            <ul class="list-unstyled">
              <li class="active"><a href="admin_category.html">比赛管理 </a></li>
              <li><a href="admin_user.html">用户管理</a></li>
              <li ><a href="admin_tags.html">标签管理 </a></li>    
              <li ><a href="admin_news.html">近期新闻管理 </a></li>
              <li ><a href="admin_newsswiper.html">新闻轮播图管理 </a></li> 
              <li ><a href="admin_competitionswiper.html">比赛轮播图管理 </a></li> 
            </ul>
          </nav>

          
          <!-- 右部表格部分 -->
          <div class="content-inner"> 
            <!-- 管理标题-->
            <header class="page-header">
              <div class="container-fluid">
                <h2 class="no-margin-bottom">
                  <script>
                      var cname=getUrlParms("cname");
                      document.write(cname)
                      </script>
                ->
                <script>
                    var pname=getUrlParms("pname");
                    document.write(pname)
                    </script>
                  ->图片管理</h2>
              </div>
            </header>
            
            <!-- 表格 -->
            <section class="tables">   
              <div class="container-fluid">
                <div class="row">
                  <div class="col-lg-12">
                    <div class="card">
                      <div class="card-body">
                        <div class="table-responsive">  
                          <div>
                          <table class="table table-striped">
                            <thead>
                              <tr>
                                <th>ID</th>
                                <th>pid</th>
                                <th>图片</th>
                                <th>删除</th>
                              </tr>
                            </thead>
                            <tbody>
                              <tr v-for="bean in data2.data">
                                <td>{{bean.id}}</td>
                                <td>{{bean.pid}}</td>
                                <td><img v-bind:src="bean.img"  width="150"></td>
                                <td><a href="#nowhere"   @click="deleteBean(bean.id)"><img src="img/delete.jpg" width="30" ></a></td>
                              </tr>
                            </tbody>
                          </table>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
            </section>  

            <form align="center">
                    <input    type="file" @change="getFile($event)" id="Pic"> 
                    <button @click="submitForm($event)" class="btn btn-primary">提交</button>
            </form>

  
            <!-- 页脚-->
            <footer class="main-footer">
              <div class="container-fluid">
                <div class="row">
                  <div class="col-sm-6">
                    <p>USC-SE</p>
                  </div>
                  <div class="col-sm-6 text-right">
                    <p>Powered by <a href="https://bootstrapious.com/p/admin-template" class="external">Bootstrapious</a></p>
                </div>
              </div>
            </footer>
  
          </div>
        </div>
      </div>
      
</body>
</html>